package com.beyond.utils;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import java.util.Map;
import java.util.Set;





/**
 * 
 * 请求方法的工具类
 *
 */
public class RestClientUtils {
	
	private static WebTarget target = null;
	
	//private static Logger logger = Logger.getLogger(RestClientUtils.class);
	
	
	/**
	 * @param url 
	 * @param path
	 * @param param
	 * @param queryParam
	 * @param rtnClass
	 * @return
	 */
	public static <T> T post(String url, String path, Object param, Map<String, Object> queryParam, Class<T> rtnClass) {
		
		target = ClientBuilder.newClient().target(url);
		
		WebTarget pathTarget = target.path(path);
		
		Set<String> keys = queryParam.keySet();
		
		for (String string : keys) {
			
			pathTarget = pathTarget.queryParam(string, queryParam.get(string));
		}
		
		System.out.println("url===="+pathTarget.getUri());
		
		return pathTarget.request(MediaType.APPLICATION_JSON_TYPE)
				.post(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE), rtnClass);
	}
	
	
	public static <T> T post(String url, String path, Object param, Class<T> rtnClass){
		
		target = ClientBuilder.newClient().target(url);
		
		WebTarget pathTarget = target.path(path);
		
		System.out.println("url===="+pathTarget.getUri());	
		
		return pathTarget.request(MediaType.APPLICATION_JSON_TYPE)
				.post(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE), rtnClass);
	}

	
	/**
	 * 带头文件的请求
	 * @param url
	 * @param path
	 * @param param
	 * @param queryParam
	 * @param heads
	 *
	 * @return
	 */
	public static String post(String url, String path, Object param, Map<String, Object> queryParam,Map<String,String> heads) {
		
		target = ClientBuilder.newClient().target(url);
		
		WebTarget pathTarget = target.path(path);
		
		Set<String> keys = queryParam.keySet();
		
		for (String string : keys) {
			
			pathTarget = pathTarget.queryParam(string, queryParam.get(string));
		}
		
		System.out.println("url===="+pathTarget.getUri());	
		
		return pathTarget.request(MediaType.TEXT_PLAIN).
				header("Authorization", heads.get("auth")).
				header("Content-Type",heads.get("type")).
				post(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE), String.class);
	}
	
	
	/**
	 * @param url 请求url 
	 * @param path 请求具体路径(如:url/project/v1/xxxx)
	 * @param param z参数
	 * @return
	 */
	public static String postText(String url, String path, Object param){

		target = ClientBuilder.newClient().target(url);
		
		WebTarget pathTarget = target.path(path);

		return pathTarget.request(MediaType.TEXT_PLAIN)
				.post(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE),String.class);
	}
	
	
  public static String getPlain(String url, String path, Map<String, Object> params){
		
		target = ClientBuilder.newClient().target(url);
		
		WebTarget pathTarget = target.path(path);
		
		Set<String> keys = params.keySet();
		
		for (String string : keys) {
		
			pathTarget = pathTarget.queryParam(string, params.get(string));
		}
		
		System.out.println("get url========"+pathTarget.getUri());
		
		
		return pathTarget.request(MediaType.TEXT_PLAIN).get(String.class);
		
		//return pathTarget.request(MediaType.TEXT_PLAIN).get(rtnClass);
	}
	
	
	
	/**
	 * @param url 请求url 
	 * @param path 请求具体路径(如:url/project/v1/xxxx)
	 * @param param z请求集合参数
	 * @return
	 */
	public static <T> T get(String url, String path, Map<String, Object> params, Class<T> rtnClass){
		
		target = ClientBuilder.newClient().target(url);
		
		WebTarget pathTarget = target.path(path);
		
		Set<String> keys = params.keySet();
		
		for (String string : keys) {
		
			pathTarget = pathTarget.queryParam(string, params.get(string));
		}
		
		System.out.println("get url========"+pathTarget.getUri());
		
		return pathTarget.request().get(rtnClass);
	}
	
	
	
	
	
	
	
	public static <T> T delete(String url, String path, Map<String, Object> params, Class<T> rtnClass){
		
		target = ClientBuilder.newClient().target(url);
		
		WebTarget pathTarget = target.path(path);
		
		Set<String> keys = params.keySet();
		
		for (String string : keys) {
			
			pathTarget = pathTarget.queryParam(string, params.get(string));
		}
		
		return pathTarget.request().delete(rtnClass);
	}
	
	public static <T> T put(String url, String path, Object param, Map<String, Object> queryParam, Class<T> rtnClass){
		
		target = ClientBuilder.newClient().target(url);
		
		WebTarget pathTarget = target.path(path);
		
		Set<String> keys = queryParam.keySet();
		
		for (String string : keys) {
			
			pathTarget = pathTarget.queryParam(string, queryParam.get(string));
		}
		return pathTarget.request(MediaType.APPLICATION_JSON_TYPE)
				.put(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE), rtnClass);
	}

	
	public static <T> T get(String url,String path,Class<T> rtnClass){
		
		target = ClientBuilder.newClient().target(url);
		
		WebTarget pathTarget = target.path(path);
		
		return pathTarget.request().get(rtnClass);
	}	 
	
}
